package com.sxkiler.demo.log4j2;

import com.alibaba.fastjson.JSONObject;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.Node;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.layout.AbstractStringLayout;

import java.nio.charset.Charset;

@Plugin(name = "LogstashLayout",category = Node.CATEGORY,elementType = Layout.ELEMENT_TYPE,printObject = true)
public class LogstashLayout extends AbstractStringLayout {
    protected LogstashLayout(Charset charset) {
        super(charset);
    }

    protected LogstashLayout(Charset aCharset, byte[] header, byte[] footer) {
        super(aCharset, header, footer);
    }

    protected LogstashLayout(Configuration config, Charset aCharset, Serializer headerSerializer, Serializer footerSerializer) {
        super(config, aCharset, headerSerializer, footerSerializer);
    }

    public String toSerializable(LogEvent logEvent) {
        JSONObject obj = new JSONObject();
        String format = logEvent.getMessage().getFormat();
        Object [] paramValue = logEvent.getMessage().getParameters();
        obj.put("message",logEvent.getMessage().getFormattedMessage());
        return obj.toString();
    }
}
